package org.dce.screencapturer.objects;

import java.awt.Point;
import java.awt.Shape;
import java.awt.geom.Path2D;

import org.dce.screencapturer.actions.drawing.InsertObjectAction;
import org.dce.screencapturer.util.IconFactory;

@SuppressWarnings("serial")
public class Arrow extends DotBasedShapeObject {

	private Dot start = new Dot(true,true,true,true);
	private Dot end = new Dot(true,true,true,true);
	
	public static final class InsertAction extends InsertObjectAction {
		public InsertAction(){
			super("actions.arrow",IconFactory.getIcon("/images/box.png"), null,Arrow.class);
		}
	};
	
	public Arrow(){
		initDots(start,end);
	}
	
	protected Dot[] getDots(){
		return new Dot[]{start,end};
	}
	
	@Override
	protected Shape getShape() {
		
		Path2D path = new Path2D.Double();
		
		Point centerStart = start.getCenter();
		Point centerEnd = end.getCenter();
		
		double angle = Math.atan2(centerEnd.y - centerStart.y, centerEnd.x - centerStart.x);
		double angle1 = angle + Math.PI / 8;
		double angle2 = angle - Math.PI / 8;
		
		path.moveTo(centerStart.x, centerStart.y);
		path.lineTo(centerEnd.x, centerEnd.y);
		
		path.lineTo(centerEnd.x - 20 * Math.cos(angle1), centerEnd.y - 20 * Math.sin(angle1));
		
		path.moveTo(centerEnd.x, centerEnd.y);
		
		path.lineTo(centerEnd.x - 20 * Math.cos(angle2), centerEnd.y - 20 * Math.sin(angle2));
		
		return path;
	}

	protected boolean supportsFill(){
		return false;
	}

	protected void onInsertionStarted(){
		Point insertionFirstPoint = getInsertionFirstPoint();
		setLocation(insertionFirstPoint.x - start.getWidth()/2,
				insertionFirstPoint.y - start.getHeight()/2);
		start.setLocation(0,0);
	}
	
	@Override
	protected void onInsertionProgress() {
		Point secondPoint = getTranslatedInsertionSecondPoint();
		end.setLocation(secondPoint);
		updateBoundsOnDotMovement();
	}
	
	protected void inInsertionDone(){
		Point secondPoint = getTranslatedInsertionSecondPoint();
		end.setLocation(secondPoint);
		updateBoundsOnDotMovement();
	}
	
}
